package kr.pe.smallhope.mobismap.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InvalidObjectException;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import kr.pe.smallhope.mobismap.vo.MarkInfoVO;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

class InternalMapUtil {

    /*
     * read .txt file to upload for distance info
     */
    static ArrayList<MarkInfoVO> readMarkInfoFromTXT(
            String fileName) throws Exception {
        ArrayList<MarkInfoVO> infoList = new ArrayList<MarkInfoVO>();
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(fileName));
            String readString;
            while ((readString = in.readLine()) != null) {
                if (readString.length() == 0) {
                    continue;
                }
                String[] spiltReadString = readString.split(MapInfoConstants.DELIMETER);
                
                if (spiltReadString.length != 7) {
                    throw new InvalidObjectException("Need 7 fields!");
                }

                infoList.add(new MarkInfoVO(
                        InternalMapUtil.trim(spiltReadString[0]),
                        InternalMapUtil.trim(spiltReadString[1]),
                        InternalMapUtil.trim(spiltReadString[2]),
                        InternalMapUtil.trim(spiltReadString[3]),
                        InternalMapUtil.trim(spiltReadString[4]),
                        InternalMapUtil.trim(spiltReadString[5]),
                        InternalMapUtil.trim(spiltReadString[6])));
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (in != null) {
                in.close();
            }
        }
        return infoList;
    }

    /*
     * get data by using sax parser
     */
    static Document loadXmlString(String xmlData) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            InputSource xmlSrc = new InputSource(new StringReader(xmlData));
            Document doc = builder.parse(xmlSrc);
            return doc;
        } catch (Exception pce) {
            pce.printStackTrace();
        }
        return null;
    }

    static String getTagValue(Document root, String tagName) {
        if (root != null) {
            NodeList nList = root.getElementsByTagName(tagName);
            for (int i = 0; i < nList.getLength(); i++) {
                Node node = nList.item(i);
                if (node != null) {
                    Node child = node.getFirstChild();
                    if ((child != null) && (child.getNodeValue() != null)) {
                        return trim(child.getNodeValue());
                    }
                }
            }
        }
        return "";

    }

    static String trim(String text) {
        if (text == null) {
            return "";
        }
        
        text = text.replaceFirst("^[ \t]+", "").trim();
        return text;
    }
    
    /**
     * @param text      string
     * @param str       the substring for which to search.
     * @param fromIndex the index from which to start the search.
     * @return text.indexOf(str, fromIndex)
     * @throws Exception if return value less than zero, throw Exception
     */
    static int getIndexOf(String text, String str, int fromIndex) throws IndexOutOfBoundsException {
        int index = text.indexOf(str, fromIndex);
        if (index < 0) {
            throw new IndexOutOfBoundsException("There is no str in text.");
        }
        return index;
    }

    static byte[] getBytes(InputStream pisContents) {
        int iSize = 0;
        byte[] byteBuff = new byte[8192];
        ByteArrayOutputStream baosContents = new ByteArrayOutputStream();

        try {
            while (true) {
                iSize = pisContents.read(byteBuff);

                if (iSize == -1)
                    break;

                baosContents.write(byteBuff, 0, iSize);
            }
        } catch (Exception e) {
            return null;
        } finally {
            try {
                baosContents.close();
            } catch (Exception e) {
            }
        }

        return baosContents.toByteArray();
    }

}
